package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.sl.au.entity.User;
import com.sl.au.entity.ViewForVisitorReward;
import com.sl.au.entity.VisitorReward;
import com.sl.au.entity.VisitorRewardProcessing;
import com.sl.au.repository.ViewForVisitorRewardRepository;
import com.sl.au.repository.VisitorRewardProcessingRepository;
import com.sl.au.repository.VisitorRewardRepository;
import com.sl.au.service.VisitorRewardProcessingService;
import com.sl.au.service.VisitorRewardService;
@Service
@Transactional
public class VisitorRewardProcessingServiceImpl extends BaseServiceImpl implements VisitorRewardProcessingService{
	
	@Autowired
	VisitorRewardProcessingRepository visitorRewardProcessingRepository;
	@Autowired
	ViewForVisitorRewardRepository viewForVisitorRewardRepository;
	
	
	@Resource
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
	@Override
	public VisitorRewardProcessingRepository getRepository() {
		return this.visitorRewardProcessingRepository;
	}
	
	@Override
	public ViewForVisitorRewardRepository getViewForVisitorRewardRepository() {
		return this.viewForVisitorRewardRepository;
	}

	@Override
	public List<ViewForVisitorReward> getUserPage(int start, int length, String key, String role, String orderName, String dir) {
		Page<ViewForVisitorReward> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if(StringUtils.hasText(key)) {
			pages = viewForVisitorRewardRepository.getPageBykey(key, role, pageable);
		}else {
			pages = viewForVisitorRewardRepository.getPage(role, pageable);
		}
		return pages.getContent();
	}

	@Override
	public int getUserPageCount(String key, String role) {
		int count = 0;
		if(StringUtils.hasText(key)) {
			count = viewForVisitorRewardRepository.getPageBykeyCount(key, role);
		}else {
			count = viewForVisitorRewardRepository.getPageCount(role);
		}
		return count;
	}

	@Override
	public List<VisitorRewardProcessing> getProcessingPage(int start, int length, String key, String userId, String orderName, String dir) {
		Page<VisitorRewardProcessing> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if(StringUtils.hasText(key)) {
			pages = visitorRewardProcessingRepository.getPageBykey(key, userId, pageable);
		}else {
			pages = visitorRewardProcessingRepository.getPage(userId, pageable);
		}
		return pages.getContent();
	}
	
	@Override
	public int getProcessingPageCount(String key, String userId) {
		int count = 0;
		if(StringUtils.hasText(key)) {
			count = visitorRewardProcessingRepository.getPageBykeyCount(key, userId);
		}else {
			count = visitorRewardProcessingRepository.getPageCount(userId);
		}
		return count;
	}

}
